package io.awen.modules.sign.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.awen.common.utils.PageUtils;
import io.awen.common.utils.Query;
import io.awen.modules.sign.dao.SignFleetDao;
import io.awen.modules.sign.entity.SignFleetEntity;
import io.awen.modules.sign.qo.SignFleetQo;
import io.awen.modules.sign.service.SignFleetService;
import io.awen.modules.sys.entity.SysUserEntity;
import io.awen.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("signFleetService")
public class SignFleetServiceImpl extends ServiceImpl<SignFleetDao, SignFleetEntity> implements SignFleetService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageUtils queryPage(SignFleetQo qo) {
        IPage<SignFleetEntity> page = this.page(
                new Query<SignFleetEntity>().getPage(qo),
                new QueryWrapper<SignFleetEntity>().lambda()
                        .eq(qo.getCtUnitId() != null, SignFleetEntity::getCtUnitId, qo.getCtUnitId())
                        .ge(qo.getStartTime() != null, SignFleetEntity::getCreateTime, qo.getStartTime())
                        .le(qo.getEndTime() != null, SignFleetEntity::getCreateTime, qo.getEndTime())
                        .like(StringUtils.isNotBlank(qo.getFleetName()), SignFleetEntity::getFleetName, qo.getFleetName())
                        .orderByDesc(SignFleetEntity::getId)
        );

        for (SignFleetEntity entity : page.getRecords()) {
            SysUserEntity user = sysUserService.getById(entity.getPrincipalId());
            entity.setPrincipal(user);
        }

        return new PageUtils(page);
    }

}